package com.jingtuo.android.common;

import java.io.File;

import com.jingtuo.android.common.os.AsyncBitmap;
import com.jingtuo.android.common.os.BitmapInfo;
import com.jingtuo.android.common.utils.ImageUtils;
import com.jingtuo.android.common.widget.ImageView;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Environment;
import android.support.v4.util.LruCache;

public class CacheManager {

	private static CacheManager cacheManager;
	
	private LruCache<String, Bitmap> lruCache;
	
	private File diskCache;
	
	public synchronized static CacheManager getInstance(String appName){
		if(cacheManager==null){
			cacheManager = new CacheManager(appName);
		}
		return cacheManager;
	}
	
	public CacheManager(String appName) {
		// TODO Auto-generated constructor stub
		int maxMemory = (int)(Runtime.getRuntime().maxMemory()/1024);

	    // Use 1/8th of the available memory for this memory cache.
		int cacheSize = maxMemory / 8;
		lruCache = new LruCache<String, Bitmap>(cacheSize){
			@Override
			protected int sizeOf(String key, Bitmap value) {
				return ImageUtils.getBytes(value)/1024;
			}
		};
		if(Environment.getExternalStorageDirectory()!=null){
			String path = "";
			if(appName==null||appName.equals("")){
				path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/temp";
			}else{
				path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + appName + "/temp";
			}
			diskCache = new File(path);
			if(!diskCache.exists()){
				diskCache.mkdirs();
			}
		}
	}
	
	/**
	 * 将Bitmap放入缓存
	 * @param key
	 * @param value
	 */
	public synchronized void put(String key, Bitmap value){
		lruCache.put(key, value);
	}
	
	/**
	 * 从缓存中读取Bitmap
	 * @param key
	 * @return
	 */
	public synchronized Bitmap get(String key){
		return lruCache.get(key);
	}
	
	
	public synchronized void setImageView(ImageView imageView, BitmapInfo bitmapInfo){
		if(imageView!=null&&bitmapInfo!=null){
			AsyncBitmap asyncBitmap = imageView.getAsyncBitmap();
			if(asyncBitmap==null){
				Bitmap bitmap = get(bitmapInfo.getPath());
				if(bitmap!=null){//软缓存中存在
					imageView.setImageBitmap(bitmap);
				}else{//
					asyncBitmap = new AsyncBitmap(this, imageView, bitmapInfo);
					imageView.setAsyncBitmap(asyncBitmap);
					imageView.getAsyncBitmap().execute();
				}
			}else{
				if(!asyncBitmap.getBitmapInfo().getPath().equals(bitmapInfo.getPath())){
					if(asyncBitmap.getStatus()==AsyncTask.Status.RUNNING){
						asyncBitmap.cancel(true);
					}
					asyncBitmap = new AsyncBitmap(this, imageView, bitmapInfo);
					imageView.setAsyncBitmap(asyncBitmap);
					imageView.getAsyncBitmap().execute();
				}
			}
		}
	}
	
	/**
	 * 清除硬缓存
	 */
	public void clearDiskCache(){
		if(diskCache!=null&&diskCache.exists()){
			File[] files = diskCache.listFiles();
			if(files!=null){
				for (int i = 0; i < files.length; i++) {
					files[i].delete();
				}
			}
		}
	}
	
	/**
	 * 根据文件名查找缓存文件
	 * @param name
	 * @return
	 */
	public File getCacheFile(String name){
		File file = null;
		if(diskCache!=null&&diskCache.exists()){
			File[] files = diskCache.listFiles();
			if(files!=null){
				for (int i = 0; i < files.length; i++) {
					String fileName = files[i].getName();
					if(fileName.equals(name)){
						file = files[i];
						break;
					}
				}
			}
		}
		return file;
	}
	
	/**
	 * 清除软存
	 */
	public void clearLruCache(){
		if(lruCache!=null){
			lruCache.evictAll();
		}
	}
	
	/**
	 * 获得磁盘缓存文件夹路径
	 * @return
	 */
	public String getDiskCacheDir(){
		if(diskCache!=null&&diskCache.exists()){
			return diskCache.getAbsolutePath();
		}else{
			return null;
		}
	}
	
}
